# 进程/线程

# import os

# print('pid: %s' % os.getpid())
# # 创建子进程
# cp = os.fork()
# # fork方法会回调两次

# 使用进程池, 启动大量子进程

from multiprocessing import Pool
import os, time, random

# print(time.time())

def long_time_job(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (name, (end - start)))

if __name__ == "__main__":
    print('Parent process %s.' % os.getpid())
    # 启动4个子进程
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_job, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()